package com.sl.water.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sl.water.dto.ForecastSchemeDTO;
import lombok.NonNull;
import org.springframework.stereotype.Service;
import com.sl.water.entity.ForecastScheme;
import com.sl.water.dao.ForecastSchemeDao;
import com.sl.water.service.ForecastSchemeService;

/**
 * <h2>预报方案库表 服务实现类</h2>
 * <p>实现类生成时间：2025-05-19 11:02:19</p>
 *
 * <p><b>版本信息：</b></p>
 * <ul>
 *     <li>版本 1.0.0 - 初始版本</li>
 * </ul>
 *
 * <p><b>更新时间：</b></p>
 * <ul>
 *     <li>2025-05-19 11:02:19 - 创建类并生成表实体。</li>
 * </ul>
 *
 * @author zhaobo2020
 * @version 1.0.0
 * @since 2025-05-19
 */
@Service
public class ForecastSchemeServiceImpl extends ServiceImpl<ForecastSchemeDao, ForecastScheme> implements ForecastSchemeService {


    @Override
    public ForecastScheme queryAutoForecastScheme() {
        LambdaQueryWrapper<ForecastScheme> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ForecastScheme::getType, 10);
        queryWrapper.eq(ForecastScheme::getStatus, 2);
        queryWrapper.orderByAsc(ForecastScheme::getCreatedTime);
        queryWrapper.notExists("select 1 from rehearsal_scheme where forecast_id=forecast_scheme.id");
        queryWrapper.last("LIMIT 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @NonNull
    public ForecastSchemeDTO getForecastSchemeDTO() {
        LambdaQueryWrapper<ForecastScheme> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ForecastScheme::getType, 10)
                .eq(ForecastScheme::getStatus, 2)
                .orderByDesc(ForecastScheme::getCreatedTime)
                .last("LIMIT 1");
        ForecastScheme forecastScheme = this.getOne(lambdaQueryWrapper);
        JSONObject resultJson = JSONUtil.parseObj(forecastScheme.getResultJson());
        resultJson.set("id", forecastScheme.getId());
        if (forecastScheme != null) {
            return JSONUtil.toBean(resultJson, ForecastSchemeDTO.class);
        }
        return new ForecastSchemeDTO();

    }
}

